function $(id){
	return document.getElementById(id);
}
function checkemail(email)
{
	if(email.search(/^\w+((-\w+)|(\.\w+))*\@\w+((-\w+)|(\.\w+))*\.\w+$/) != -1)
	{
		return true;
	}
	else
	{
		return false;
	}
}

if(document.uniqueID && !window.XMLHttpRequest){
    try {document.execCommand('BackgroundImageCache',false,true);}
    catch(e){};
};
if(!EnterBay){var EnterBay={};}
var YUD = YAHOO.util.Dom;
var YUE = YAHOO.util.Event;
var YUC = YAHOO.util.Connect;
var EB = EnterBay;

EB.Util = {
    Contains:function(a, b){
        if (typeof a === 'string' || !a) {a = document.getElementById(a);}
        if (typeof b === 'string' || !b) {b = document.getElementById(b);}
        return a.contains ? 
        a != b && a.contains(b) :
        !!(a.compareDocumentPosition(b) & 16);
    },
    setDDLIndexByValue:function(oDDL,value){
        var opts = oDDL.getElementsByTagName('option');
        for(var i=0,l=opts.length;i<l;i++){
            if(opts[i].value==value){
                oDDL.selectedIndex = i;
                return;
            }
        }
    },
    textCounter:function(str){
        return str?str.replace(/[^\x00-\xff]/g,'**').length:0;
    },
    enWordBreaker:function(str,length){
        if (str.length <= length) {
            return str;
        }
        else {
            var strb = "";
            var l = 0;
            var schar;
            for (var i = 0; schar = str.charAt(i); i++) {
                strb += schar;
                l ++;
                if (l >= length) {
                    strb += "<br />\n";
                    l = 0;
                }
            }
        }
        return strb;
    },
    imgPreLoader:function(){
        for (var i=0;i<arguments.length;i++) {
            var imageObj = new Image();
            imageObj.src = arguments[i];
        }
    }
};
EB.Class = {
};
EB.Module = {
	bigADplayer:function(dModule){
		var dImgCter = YUD.getElementsByClassName('adul','ul',dModule)[0];
        var dImgs = dImgCter.getElementsByTagName('li');
        var dBtnCter = YUD.getElementsByClassName('btn','ul',dModule)[0];
        var dBtns = dBtnCter.getElementsByTagName('li');
        var oAnim;
        var oImgLength = dImgs.length-1, iCurrentIndex = 0;
        var timer = null;
        var bAniming = false;
        var slide = true;
        
        if(slide){
        	YUD.addClass(dModule, 'slide');
        	dImgCter.style.height = dImgs.length * 285 + 'px';
        }
        
        var stopPlay = function(){
        	clearInterval(timer);
        };
        var autoPlay = function(){
        	stopPlay();
        	timer = window.setInterval(function(){
        		if(bAniming){return;}
	        	if(iCurrentIndex>=oImgLength){
	        		iCurrentIndex = 0;
	        	}else{
	        		iCurrentIndex++;
	            }
	        	playTo(iCurrentIndex);
		        	
	        },5000);
        };
        
        var playTo = function(index){
        	if(bAniming){return;}
        	stopPlay();
        	dImgs[index].style.zIndex = oImgLength+1;
        	dImgs[index>=oImgLength?0:index+1].style.zIndex = oImgLength;
        	if(slide){
        		oAnim = new YAHOO.util.Anim(dImgCter,{top:{to:-index*285}},0.5,YAHOO.util.Easing.easeOut);
        	}else{
        		oAnim = new YAHOO.util.Anim(dImgs[index],{opacity:{to:0,from:1}},0.8,YAHOO.util.Easing.easeNone);
        	}
			oAnim.animate();
			bAniming = true;
			oAnim.onComplete.subscribe(function() { 
	        	dImgs[index].style.zIndex = 0;
	        	YUD.setStyle(dImgs[index],'opacity','1');
	        	iCurrentIndex = index;
	        	bAniming = false;
	        	autoPlay();
		    });
			YUD.removeClass(dBtns, 'on');
			dBtns[index].className = 'on';
        };
        
        YUE.on(dBtns,'click',function(e){
            YUE.stopEvent(e);
            playTo(YUE.getTarget(e).getAttribute('index')-1);
        });
        
        autoPlay();
	},
    productImgPlayer:function(dModule){
        var oAnim,isAniming=false,currentSet=0;
        var dBImg = YUD.getElementsByClassName('img','div',dModule)[0].getElementsByTagName('img')[0];
        var dImgCter = YUD.getElementsByClassName('imgct','div',dModule)[0];
        var dImgCterIn = YUD.getElementsByClassName('imgctin','div',dImgCter)[0];
        var dImgs = dImgCterIn.getElementsByTagName('a');
        var iMoveSet = dImgs.length-6;
        var dBtnUp = YUD.getElementsByClassName('up','a',dModule)[0];
        var dBtnDown = YUD.getElementsByClassName('down','a',dModule)[0];
        var dNum = YUD.getElementsByClassName('imgnum','span',dModule)[0];
        
        var Move = function(l){
            if(isAniming||(currentSet<=0&&l>0)||(currentSet>=iMoveSet&&l<0)){return;}
            var t = dImgCterIn.offsetLeft;//dImgCterIn.offsetTop;
            oAnim = new YAHOO.util.Anim(dImgCterIn,{left:{to:t+l}},0.3,YAHOO.util.Easing.easeNone);
            oAnim.onComplete.subscribe(function() { 
                isAniming = false;
                if(l>0){
                    currentSet--;
                }else{
                    currentSet++;
                }
                btnOnOff();
            });
            isAniming = true;
			oAnim.animate();

        };
        var btnOnOff = function(){
            if (currentSet <= 0) {
                YUD.addClass(dBtnUp, 'disable');
                dBtnUp.disable = true;
            }else{
                YUD.removeClass(dBtnUp,'disable');
                dBtnUp.disable = false;
            } 
            if (currentSet >= iMoveSet) {
                YUD.addClass(dBtnDown, 'disable');
                dBtnDown.disable = true;
            }else{
                YUD.removeClass(dBtnDown,'disable');
                dBtnDown.disable = false;
            }
        };
        var MoveUp = function(e){
            YUE.stopEvent(e);
            Move(56);
        };
        var MoveDown = function(e){
            YUE.stopEvent(e);
            Move(-56);
        };
        YUE.on(dImgs,'click',function(e){
            YUE.stopEvent(e);
            if(this.firstChild.tagName.toLowerCase()=='img'){
                dBImg.src = this.firstChild.getAttribute('bimg');
                YUD.removeClass(dImgs,'on');
                YUD.addClass(this,'on');
                dNum.innerHTML = this.getAttribute('idx');
            }
        });
        YUE.on(dBtnUp,'click',MoveUp);
        YUE.on(dBtnDown,'click',MoveDown);
        btnOnOff();
    },
    productCateNav:function(dModule){
        var iDefSet = document.getElementById('defSet').value-2;
        var oAnim,isAniming=false,currentSet=iDefSet<0?0:iDefSet;
        var dImgCter = YUD.getElementsByClassName('nav','div',dModule)[0];
        var dImgCterIn = dImgCter.getElementsByTagName('ul')[0];
        var dImgs = dImgCterIn.getElementsByTagName('li');
        var iMoveSet = dImgs.length-3;
        var dBtnLeft = YUD.getElementsByClassName('mleft','a',dModule)[0];
        var dBtnRight = YUD.getElementsByClassName('mright','a',dModule)[0];
        var Move = function(l){
            if(isAniming||(currentSet<=0&&l>0)||(currentSet>=iMoveSet&&l<0)){return;}
            var t = dImgCterIn.offsetLeft;
            oAnim = new YAHOO.util.Anim(dImgCterIn,{left:{to:t+l}},0.3,YAHOO.util.Easing.easeNone);
            oAnim.onComplete.subscribe(function() { 
                isAniming = false;
                if(l>0){
                    currentSet--;
                }else{
                    currentSet++;
                }
                btnOnOff();
            });
            isAniming = true;
			oAnim.animate();

        };
        var btnOnOff = function(){
            if (currentSet <= 0) {
                YUD.addClass(dBtnLeft, 'disable');
            }else {
                YUD.removeClass(dBtnLeft,'disable');
            }
            if (currentSet >= iMoveSet) {
                YUD.addClass(dBtnRight, 'disable');
            }else{
                YUD.removeClass(dBtnRight,'disable');
            }
        };
        var MoveLeft = function(e){
            YUE.stopEvent(e);
            Move(300);
        };
        var MoveRight = function(e){
            YUE.stopEvent(e);
            Move(-300);
        };
        YUE.on(dBtnLeft,'click',MoveLeft);
        YUE.on(dBtnRight,'click',MoveRight);
        btnOnOff();
    },
    SalesNet:function(dModule){
        EB.Util.imgPreLoader(
            'images/map/china.gif',
            'images/map/hong-kong.gif',
            'images/map/taiwan.gif',
            'images/map/japan.gif',
            'images/map/korea.gif',
            'images/map/singapore.gif',
            'images/map/thailand.gif',
            'images/map/malaysia.gif',
            'images/map/australia.gif',
            'images/map/indonesia.gif',
            'images/map/usa&canada.gif',
            'images/map/united-kingdom.gif',
            'images/map/france.gif',
            'images/map/netherlands.gif',
            'images/map/philippines.gif',
            'images/map/Russia.gif'
        );
        
        var dTriggers = YUD.getElementsByClassName('ct','li',dModule);
        var dCurrentImg = YUD.getElementsByClassName('currentimg','img',dModule)[0];
        var dInfo = YUD.getElementsByClassName('info','div',dModule)[0];
        var dTip = YUD.getElementsByClassName('tip','div',dModule)[0];
        var oPreCT = null;
        var oAnim;
        
        YUE.on(dTriggers,'mouseover',function(e){
            YUE.stopEvent(e);
            if(oPreCT){
            	YUD.removeClass(oPreCT, 'on');
            }
            dCurrentImg.src = 'images/map/'+this.getAttribute('ct');
            dCurrentImg.style.display = 'block';
            YUD.addClass(this, 'on');
            if(this.getElementsByTagName('ul')[0]){
                dInfo.innerHTML = '<ul>' + this.getElementsByTagName('ul')[0].innerHTML + '</ul>';
                dInfo.style.display = 'block';
            }
            oPreCT = this;
        });
        YUE.on(dInfo,'mouseover',function(e){
            YUE.stopEvent(e);
            var target = YUE.getTarget(e);
            if(target.tagName.toLowerCase() == 'a'){
            	//dTip.innerHTML = target.getAttribute('msg') + '&nbsp;';
            	dTip.innerHTML = target.getElementsByTagName('span')[0].innerHTML + '&nbsp;';
            	dTip.style.top = YUD.getY(target) - YUD.getY(dModule) + 20 + 'px';
            	dTip.style.left = YUD.getX(target) - YUD.getX(dModule) + 20 + 'px';
            	dTip.style.display = 'block';
            	oAnim = new YAHOO.util.Anim(dTip,{opacity:{to:1,from:0}},0.3,YAHOO.util.Easing.easeNone);
                oAnim.onComplete.subscribe(function() { 
                });
                oAnim.animate();
            }
        });
        YUE.on(dInfo,'mouseout',function(e){
            YUE.stopEvent(e);
            dTip.style.display = 'none';
        });
    },
    Buy:function(dModule){
        var dBuyDiv = YUD.getElementsByClassName('buy','div',dModule);
        if(dBuyDiv.length<1){return;}
        var dForm = dBuyDiv[0].getElementsByTagName('form')[0];
        if(!dForm){return;}
        var dBtn = YUD.getElementsByClassName('btn','a',dBuyDiv[0]);
        YUE.on(dBtn,'click',function(e){
            YUE.stopEvent(e);
            dForm.submit();
        });
    },
    PictureList:function(dModule){
        var dLis = dModule.getElementsByTagName('li');
        YUE.on(dLis,'mouseover',function(){YUD.addClass(this,'on');});
        YUE.on(dLis,'mouseout',function(){YUD.removeClass(dLis,'on');});
    },
    NewsLetter:function(dModule){
        var dBtnCl = YUD.getElementsByClassName('cancel','a',dModule)[0];
        var dBtnOk = YUD.getElementsByClassName('submit','a',dModule)[0];
        var dIptName = YUD.getElementsByClassName('name','input',dModule)[0];
        var dIptEmail = YUD.getElementsByClassName('email','input',dModule)[0];
        
        var close = function(){
        	dIptName.value = '';
    		dIptEmail.value = '';
            dModule.style.display = 'none';
        	dBtnOk.innerHTML = "OK";
        };
        YUE.on(dBtnCl,'click',function(e){
        	YUE.stopEvent(e);
        	close();
        });
        YUE.on(dBtnOk,'click',function(e){
        	YUE.stopEvent(e);
        	if(dBtnOk.innerHTML!='OK'){return;}
        	YUD.removeClass(dIptName, 'error');
        	YUD.removeClass(dIptEmail, 'error');
        	if(dIptName.value.length<1){
        		YUD.addClass(dIptName, 'error');
        		return;
        	}
	        if(dIptEmail.value.length<1){
	    		YUD.addClass(dIptEmail, 'error');
	    		return;
	    	}
        	if(!checkemail(dIptEmail.value)){
        		//alert('Email address must be in a proper format');
        		YUD.addClass(dIptEmail, 'error');
        		return;
        	}
        	var oCallBack = {
                success:function(oResponse){
                    if(oResponse.responseText == '')return;
                    if (oResponse.responseText > 0){
                    	dBtnOk.innerHTML = "Successful!";
                    }
                    else {
                    	dBtnOk.innerHTML = "Error...";
                    }
                    setTimeout(function(){
                		close();
                	},2000);
                },
                failure:function(oResponse){},
                timeout:15000
            };
            YUC.asyncRequest('POST','newsletter.php',oCallBack,'name=' + dIptName.value + '&email=' + dIptEmail.value);
        });
    },
    lnkToNewsLetter:function(dModule){
        var md_newslter = document.getElementById('md_newslter');
        YUE.on(dModule,'click',function(){
        	md_newslter.style.display = 'block';
        	md_newslter.style.height = '0';
        	var anim = new YAHOO.util.Anim(md_newslter, {
        		height:{from:0, to:200},
        		opacity:{from:0, to:1}
        	}, 0.5, YAHOO.util.Easing.easeOut);  
        	anim.animate();
        });
    }
};

(function(){
    var doWhileExist = function(sModuleId,oFunction){
        if(document.getElementById(sModuleId)){oFunction(document.getElementById(sModuleId));}
    };
    doWhileExist('md_bigad',EB.Module.bigADplayer);
    doWhileExist('md_pdimgs',EB.Module.productImgPlayer);
    doWhileExist('md_catenav',EB.Module.productCateNav);
    doWhileExist('md_map',EB.Module.SalesNet);
    doWhileExist('md_pdinfo',EB.Module.Buy);
    doWhileExist('md_piclist',EB.Module.PictureList);
    doWhileExist('md_newslter',EB.Module.NewsLetter);  
    doWhileExist('lnk_newslter',EB.Module.lnkToNewsLetter);  
})();




